const app = getApp();

var type = 'add';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    img_file_name: [],
    img_file_name2:[],
    shopInfo:{
      pics:[]
    }
  },
  backto: function (e) {
    wx.navigateBack({})
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    const token = extConfig.appid;
    this.setData({
      token: token,
      shop_id: options.shop_id,
      wid: options.wid
    })
    wx.request({
      url: 'https://www.58zltc.com/api.php/Index/getShopInfo?token=' + that.data.token,
      data:{
        id: options.shop_id
      },
      header: {
        "Content-Type": "application/json"
      },
      method: 'GET',
      success: function (res) {
        // var data = that.data.img_file_name
        // var length = data.length
        // data[length] = res.data.shop_advimg
        that.setData({
          shopInfo: res.data,
          img_file_name: res.data.pics,
          img_file_name2: res.data.shop_banner,
          shop_video: res.data.shop_video
        })
        console.log(that.data)
        
      }
    })
  },
  //轮播
  bindUploadImgl: function (e) {
    var data = e.currentTarget.dataset;
    var that = this;
    var list = that.data;
    wx.chooseImage({
      count: data.num > 1 ? 9 : 1,
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        wx.showLoading({
          title: '图片上传中……',
        })
        tempFilePaths.forEach(function (item, idx) {
          wx.uploadFile({
            url: 'https://www.58zltc.com/api.php/Index/uploadImage?token=' + that.data.token,
            filePath: item,
            name: 'file',
            success: function (res) {
              var out = JSON.parse(res.data);
              if (out.err_code > 0) {
                wx.showToast({
                  title: '上传失败！',
                })
              } else {
                //list.forEach(function (value, index) {
                var pics = list.img_file_name2;
                
                var file_name = out.data.domain;
                if (pics != null && pics != "") {
                  pics.push(file_name);
                } else {
                  pics = [file_name];
                }
                //list.img_file_name2 = pics;

                //})
                that.setData({
                  img_file_name2: pics,
                  file_name2: pics
                })
                console.log(that.data);
              }

              if (idx == tempFilePaths.length - 1) {
                wx.hideLoading();
              }
              //do something
            }
          })
        })
      }
    })
  },
  bindUploadImg: function (e) {
    var data = e.currentTarget.dataset;
    var that = this; 
    var list = that.data;
    wx.chooseImage({
      count: data.num > 1 ? 9 : 1,
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        wx.showLoading({
          title: '图片上传中……',
        })
        tempFilePaths.forEach(function (item, idx) {
          wx.uploadFile({
            url: 'https://www.58zltc.com/api.php/Index/uploadImage?token=' + that.data.token,
            filePath: item,
            name: 'file',
            success: function (res) {
              var out = JSON.parse(res.data);
              console.log(out)
              if (out.err_code > 0) {
                wx.showToast({
                  title: '上传失败！',
                })
              } else {
                //list.forEach(function (value, index) {
                var pics = list.img_file_name;
                var file_name = out.data.domain;
                if (pics != null && pics!="") {
                  pics.push(file_name);
                } else {
                  pics = [file_name];
                }
                //list.img_file_name2 = pics;

                //})
                that.setData({
                  img_file_name: pics,
                  file_name: pics
                })
                console.log(that.data);
              }
              if (idx == tempFilePaths.length - 1) {
                wx.hideLoading();
              }
              //do something
            }
          })
        })
      }
    })
  },
  formSubmit: function (e) {
    var that = this;
    console.log(e)
    app.addFormId(e.detail.formId);
    var shop_link = e.detail.value.shop_link;
    var file_name = this.data.file_name;//相册
    var file_name2 = this.data.file_name2;//轮播
    wx.request({
      url: 'https://www.58zltc.com/api.php/Index/uploadShopImg?token=' + that.data.token,
      data: {
        shop_id: that.data.shop_id,
        link: shop_link,
        file_name:  file_name,
        file_name2: file_name2 //店铺轮播
      },
      header: {
        "Content-Type": "application/json"
      },
      method: 'POST',
      success: function (res) {
        if (res.data) {
          wx.showModal({
            title: '提交成功'
          })
        } else {
          wx.showModal({
            title: '提交失败',
            content: '未做修改或请求失败'
          })
        }
      }
    })
  },
  delImg: function (e) {
    let that = this;
    var flash    = that.data.img_file_name2;
    var index    = e.currentTarget.dataset.index;
    var img_path = e.currentTarget.dataset.path;
    var shop_id  = that.data.shop_id;
    flash.splice(index, 1);
    that.setData({
      img_file_name2: flash
    })
    wx.showToast({
      title: '删除成功',
      icon: 'success',
      duration: 1000
    })
    wx.request({
      url: 'https://www.58zltc.com/api.php/Shop/delShopBanner?token=' + that.data.token,
      data: {
        shop_id : shop_id,
        path    : img_path
      },
      header: {
        "Content-Type": "application/json"
      },
      method: 'GET',
      success: function (res) {
        if (!res.data) {
          wx.showToast({
            title: '网络繁忙',
            icon: 'none',
            duration: 1500
          })
        }
      }
    })
  },
  delImgP: function (e) {
    let that = this;
    var flash = that.data.img_file_name;
    var index = e.currentTarget.dataset.index;
    var img_path = e.currentTarget.dataset.path;
    var shop_id = that.data.shop_id;
    console.log(img_path)
    flash.splice(index, 1);
    that.setData({
      img_file_name: flash
    })
    wx.showToast({
      title: '删除成功',
      icon: 'success',
      duration: 1000
    })
    wx.request({
      url: 'https://www.58zltc.com/api.php/Shop/delShopPicture?token=' + that.data.token,
      data: {
        shop_id: shop_id,
        path: img_path
      },
      header: {
        "Content-Type": "application/json"
      },
      method: 'GET',
      success: function (res) {
        if (!res.data) {
          wx.showToast({
            title: '网络繁忙',
            icon: 'none',
            duration: 1500
          })
        }
      }
    })
  }
})